#ifndef VSqlData_H
#define VSqlData_H

#include "../VCommon/VIntegerList.h"
#include "VSqlMeta.h"
#include "VSqlDataMRef.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VSqlDataColumn
{
public:
	void setMeta(const VSqlMeta &v) { m_meta = v;}
	VSqlMeta meta() const { return m_meta;}
	
	QString displayName() const;

private:
	VSqlMeta m_meta;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlDataColumnList : public QList<VSqlDataColumn>
{
public:
	VSqlDataColumnList();

	void setFrom(const QList<VSqlMeta> &metaFieldList);

	int indexOf(ESqlItemType eItemType) const;

	VSqlSharedRecordPtr newRecord() const;
	int colIdRec() const { return m_colIdRec;}

private:
	int m_colIdRec;
};

///////////////////////////////////////////////////////////////////////////////

class VSqlData
{
public:
	VSqlData(const QSqlDatabase &db) :m_db(db) {}
	~VSqlData() { clear();}
	
	void clear();
	void setFrom(const VSqlMeta &metaTable,const QList<VSqlMeta> &metaFieldList);

	VSqlMeta metaTable() const { return m_metaTable;}
	
	VSqlDataColumnList columnList() const { return m_columnList;}
	int recordCount() const { return m_recordList.count();}
	QSqlField idRecField() const;
			 
	VSqlSharedRecordPtr record_byRow(int row) const { return m_recordList[row];}
	VSqlSharedRecordPtr record_byIdRec(const QVariant &idRec);
	QString record_toDisplay(const QVariant &idRec);

	QList<VSqlSharedRecordPtr> selectRecords(const QString &sqlWhere = QString()) const;
	QList<VSqlSharedRecordPtr> selectRecords_byIdRecList(const QVariantList &idRecList) const;
	void resetRecordList();
	void setRecordList(const QList<VSqlSharedRecordPtr> &recordList);

	bool updateValue(int row,int column,const QVariant &value);
	VSqlSharedRecordPtr insertRecord();

	QVariantList idRecListFromRowList(const VIntegerList &rowList) const;
	int rowFromIdRec(const QVariant &idRec) const;
	void removeRow(int row);

	void removeRecords(const QVariantList &idRecList) const;
	
private:
	QSqlDatabase m_db;
	VSqlMeta m_metaTable;
	VSqlDataColumnList m_columnList;
	QList<VSqlSharedRecordPtr> m_recordList;
};

///////////////////////////////////////////////////////////////////////////////

#endif
